package admin.controllers;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import systems.AdsSystem;

import entities.AdsenseAdv;

import admin.forms.AdsenseAdvForm;
import admin.models.AdsenseAdvModel;

public class AdsenseAdvServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		TreeMap<String, String> check = new TreeMap<String, String>();
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String name = URLDecoder.decode(path.toLowerCase().replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		AdsenseAdvModel adsModel = AdsenseAdvModel.getInstance();
		AdsenseAdv ads = adsModel.get(name);
		
		if (ads == null) {
			ads = new AdsenseAdv();
			ads.setId(name);
		}
		
		request.setAttribute("ads", ads);
		request.setAttribute("check", check);
		request.setAttribute("name", name);
		try {
			request.getRequestDispatcher("/admin/pages/adsense_adv_form.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String name = URLDecoder.decode(path.toLowerCase().replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		AdsenseAdvModel advModel = AdsenseAdvModel.getInstance();
		AdsenseAdv ads = advModel.get(name);
		
		if (ads == null) {
			ads = new AdsenseAdv();
			ads.setId(name);
		}
		
		//Check value valid form
		TreeMap<String, Object> forms = (TreeMap<String, Object>) AdsenseAdvForm.checkValid(request, ads); 
		ads = (AdsenseAdv) forms.get("ads");
		TreeMap<String, String> check = (TreeMap<String, String>) forms.get("checkValid");
		
		if (check.size() == 0) {
			advModel.insert(ads);
			request.setAttribute("success", "Change data sucess.");
			
			//Set ads system
			setAds(ads);
		}
		
		request.setAttribute("ads", ads);
		request.setAttribute("check", check);
		request.setAttribute("name", name);
		try {
			request.getRequestDispatcher("/admin/pages/adsense_adv_form.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void setAds(AdsenseAdv ads) {
		if (ads.getId().equals("160x600")) {
			AdsSystem.setAds160x600(ads);
		} 
		
		if (ads.getId().equals("200x200")) {
			AdsSystem.setAds200x200(ads);
		}
		
		if (ads.getId().equals("234x60")) {
			AdsSystem.setAds234x60(ads);
		}
		
		if (ads.getId().equals("250x250")) {
			AdsSystem.setAds250x250(ads);
		}
		
		if (ads.getId().equals("300x250")) {
			AdsSystem.setAds300x250(ads);
		}
		
		if (ads.getId().equals("300x600")) {
			AdsSystem.setAds300x600(ads);
		}
		
		if (ads.getId().equals("336x280")) {
			AdsSystem.setAds336x280(ads);
		}
		
		if (ads.getId().equals("468x15")) {
			AdsSystem.setAds468x15(ads);
		}
		
		if (ads.getId().equals("468x60")) {
			AdsSystem.setAds468x60(ads);
		}

		if (ads.getId().equals("728x15")) {
			AdsSystem.setAds728x15(ads);
		}
		
		if (ads.getId().equals("728x90")) {
			AdsSystem.setAds728x90(ads);
		}
	}
}
